package com.yd.mvc.mobile.couponController;

import com.yd.app.service.mobile.couponService.MCouponService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.UserCoupon;
import com.yd.domain.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 优惠券
 * Created by sunxianhui on 2018/3/9.
 */
@RestController
@RequestMapping(value = "/mapi/userCoupon")
public class MCouponController {
    @Autowired
    MCouponService mCouponService;
 /*   @Autowired
    MyHandler handler;*/
    /**
     * 根据条件分页查询
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getUserCouponList", method = RequestMethod.GET)
    public List<UserCoupon> getCouponList(HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        List<UserCoupon> list = null;
        try {
            if(u!=null){
                list = mCouponService.getCouponList(u.getId());
                for(int i =0;i<list.size();i++){
                    if (list.get(i).getCoupon()==null) {
                        list.remove(i);
                    }
                }

            }else{
                throw new BusinessException(500, "尚未登录","no-login");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(500, "获取用户优惠券信息失败");
        }
        return list;
    }
  /*  @RequestMapping(value = "/getUserMessage", method = RequestMethod.GET)
    public String  getCouponMessage(HttpServletRequest request, HttpServletResponse response) throws BusinessException, IOException {
        boolean hasSend = handler.sendMessageToUser(4, new TextMessage("发送一条小xi"));
        System.out.println(hasSend);
        return "message";
    }*/
}
